﻿using Business.BookManagement;
using Business.Models;
using Business.WarehouseManagement.Dto;
using SixLabors.ImageSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using WkHtmlToPdfDotNet;
using XCZ;
using Yitter.IdGenerator;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Business.WarehouseManagement
{

    public class WarehouseAppService : BusinessWorkFlowAppService, IWarehouseAppService
    {
        private const string FormName = "Warehouse";
        private IRepository<AddWarehouse,Guid> _repository;
        private IRepository<AddWarehouseList, Guid> _repositoryList;
        private IRepository<Warehouses, Guid> _warehouses;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">入库</param>
        /// <param name="repositoryList">入库明细</param>
        /// <param name="warehouses">仓库表</param>
        public WarehouseAppService(IRepository<AddWarehouse, Guid> repository, IRepository<AddWarehouseList, Guid> repositoryList, IRepository<Warehouses, Guid> warehouses)
        {
            _repository=repository;
            _repositoryList=repositoryList;
            _warehouses=warehouses;
        }

        #region 入库增删查

        /// <summary>
        /// 创建或更新入库
        /// </summary>
        /// <param name="input">入库信息</param>
        /// <returns></returns>
        public async Task<AddWarehouseDto> CreateOrUpdate(AddWarehouseDto input)
        {
            AddWarehouse result = null;
            if(!input.Id.HasValue)
            {
                //添加入库表
                input.Id = GuidGenerator.Create();
                input.Code = YitIdHelper.NextId().ToString();
                AddWarehouse house = new AddWarehouse()
                {
                    Name = input.Name,
                    AddTime = input.AddTime,
                    AWName = input.AWName,
                    AwType = input.AwType,
                    AWSumPrice = input.AWSumPrice,
                    AWCount = input.AWCount,
                };

                foreach(var item in input.WarehouseList)
                {
                    //添加入库明细
                    AddWarehouseList warehouse = new AddWarehouseList()
                    {
                        ShoppingId = item.ShoppingId,
                        SpName = item.SpName,
                        SpNumber = item.SpNumber,
                        SpPrice = item.SpPrice,
                        SpUnit = item.SpUnit,
                        SpCount = item.SpCount,
                        SpSumPrice = item.SpSumPrice,
                        WarehouseId = input.Id,
                    };

                    await _repositoryList.InsertAsync(warehouse);

                    //添加仓库表
                    Warehouses warehouses = new Warehouses();
                    //判断是否存在该产品
                    if(warehouses.ShoppingName != warehouse.SpName)
                    {
                        warehouses.ShoppingId = warehouse.ShoppingId;
                        warehouses.ShoppingName = warehouse.SpName;
                        warehouses.ShoppingCount =  warehouse.SpCount;
                        //保存
                        await _warehouses.InsertAsync(warehouses);
                    }
                    else
                    {
                        //判断
                        var infoss = await _repositoryList.FirstOrDefaultAsync(x => x.SpName == warehouses.ShoppingName);
                        //更新产品库存
                        warehouses.ShoppingCount +=  infoss.SpCount;
                        //保存
                        await _warehouses.UpdateAsync(warehouses);
                    }
                };
                result = await _repository.InsertAsync(ObjectMapper.Map<AddWarehouseDto,AddWarehouse>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result  = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<AddWarehouse, AddWarehouseDto>(result);
        }

        //public async Task Delete(List<Guid> id)
        //{
        //    throw new NotImplementedException();
        //}

        //public async Task<AddWarehouseDto> Get(Guid id)
        //{
        //    throw new NotImplementedException();
        //}

        /// <summary>
        /// 获取所有入库信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PagedResultDto<AddWarehouseDto>> GetAll(GetWarehouseInputDto input)
        {
            var list = (from a in await _repository.GetQueryableAsync()
                        select new AddWarehouseDto()
                        {
                            Id = a.Id,
                            Code = a.Code,
                            Name = a.Name,
                            AddTime = a.AddTime,
                            AWName = a.AWName,
                            AwType = a.AwType,
                            AWSumPrice = a.AWSumPrice,
                            AWCount = a.AWCount,
                            WarehouseList = (from b in _repositoryList.GetQueryableAsync().Result
                                             where b.WarehouseId == a.Id
                                             select new AddWarehouseListDto()
                                             {
                                                 ShoppingId = b.ShoppingId,
                                                 SpName = b.SpName,
                                                 SpNumber = b.SpNumber,
                                                 SpPrice = b.SpPrice,
                                                 SpUnit = b.SpUnit,
                                                 SpCount = b.SpCount,
                                                 SpSumPrice = b.SpSumPrice,
                                             }).ToList()
                        }).ToList();
            var totalCount = list.Count();
            var items = list.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();
            return new PagedResultDto<AddWarehouseDto>(totalCount, list);
        
        }
        #endregion
    }
}
